package com.awangyh.net.wio;

import com.awangyh.net.bio.TimeServerHandler;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author: wangyanhui
 * @since: 2017/4/27
 */
public class TimeServer {

    public static void main(String[] args) throws IOException {
        int port = 8080;
        if (args != null && args.length > 0 ){
            try {
                port = Integer.valueOf(args[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            System.out.println("The time server is start in port :" + port);
            Socket socket = null;

            TimeServerHandlerExcecutePool singleExecutor = new TimeServerHandlerExcecutePool(50, 10000);

            while ( true) {
                socket = server.accept();
                singleExecutor.execute(new TimeServerHandler(socket));
            }
        } finally {
            if ( server != null) {
                System.out.println("The time server close");
                server.close();
                server = null;
            }
        }
    }
}
